package com.project.controller.web;


import com.project.entity.Class;
import com.project.entity.Department;
import com.project.entity.TeacherInfo;
import com.project.entity.User;
import com.project.myBeans.R;
import com.project.myBeans.TeacherInfoDetail;
import com.project.service.ClassService;
import com.project.service.UserService;
import com.project.service.DepartmentService;
import com.project.service.TeacherInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

@EnableAutoConfiguration
@RestController
@RequestMapping("/teacherinfo")
@Api(description = "教师信息接口", value = "teacherinfo")
public class TeacherInfoController {

    @Autowired
    TeacherInfoService teacherInfoService;

    @Autowired
    UserService userService;

    @Autowired
    DepartmentService departmentService;


    /**
     * wwh 2018/3/30
     *
     * @param userid
     * @return
     */
    @ApiOperation("通过用户id查找教师信息")
    @GetMapping("/findTeacherByUserid/{userid}")
    public R findTeacherByUserid(@PathVariable Integer userid) {
        try {
            TeacherInfo teacherInfo = teacherInfoService.findByUserid(userid);
            return R.ok().put("teacherInfo", teacherInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * @param teacherInfoDetail {teacherInfo，user，department 將系id存放在teacherInfo或department內}
     * @return
     */
    @ApiOperation("单个添加教师信息")
    @PostMapping("/addDetailOne")
    public R addDetailOne(@RequestBody TeacherInfoDetail teacherInfoDetail) {
        try {
            if (teacherInfoDetail.getDepartment().getId() == null && teacherInfoDetail.getTeacherInfo().getDepartmentid() == null) {
                return R.error("请先选择系");
            } else {
                Department department = null;
                if (teacherInfoDetail.getDepartment().getId() == null) {
                    department = departmentService.findById(teacherInfoDetail.getTeacherInfo().getDepartmentid());
                } else if (teacherInfoDetail.getTeacherInfo().getDepartmentid() == null) {
                    department = departmentService.findById(teacherInfoDetail.getDepartment().getId());
                }

                if (department == null) {
                    return R.error("该系不存在");
                }

                User user = teacherInfoDetail.getUser();
                if (userService.findByUserName(user.getUsername()) != null) {
//                    user = userService.findByUserName(user.getUsername());
                    return R.error("该用户已存在");
                } else {
                    user.setUniversityid(department.getUniversityid());
                    user.setSchoolid(department.getSchoolid());
                    userService.add(user);
                }

                TeacherInfo teacherInfo = teacherInfoDetail.getTeacherInfo();
                teacherInfo.setDepartmentid(department.getId());
                teacherInfo.setSchoolid(department.getSchoolid());
                teacherInfo.setUserid(user.getId());
                teacherInfoService.add(teacherInfo);
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
}
